package cn.com.demo.microservice.consumer.service.impl;

import cn.com.demo.microservice.consumer.service.IUserService;
import cn.com.demo.microservice.consumer.service.feign.IAccountAddFeginService;
import cn.com.demo.microservice.consumer.service.feign.IUserAddFeignService;
import cn.com.demo.microservice.entity.AqAccount;
import cn.com.demo.microservice.entity.AqUser;
import cn.com.demo.microservice.vo.Account;
import cn.com.demo.microservice.vo.ConvertTools;
import cn.com.demo.microservice.vo.User;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserAddFeignService userFeignService;
    @Autowired
    private IAccountAddFeginService accountAddFeginService;
    @Override
    @Transactional
    @LcnTransaction
    public User addUser(User user) {
        AqUser aqUser = ConvertTools.convert2Po(user);
        int urId = this.userFeignService.addUser(aqUser);
        if(urId > 0){
            user.setUrId(urId);

            List<Account> accountList = user.getAccountList();
            if(accountList != null && accountList.size()>0){
                Account account = accountList.get(0);
                AqAccount aqAccount = new AqAccount();
                aqAccount.setAccCode(account.getUsername());
                aqAccount.setAccPassword(account.getPassword());
                aqAccount.setAccStatus(0);
                aqAccount.setAccUrId(urId);
                aqAccount.setAccSlat("demo");

                int accId = this.accountAddFeginService.addAccount(aqAccount);

                account.setId(accId);
            }
        }
        return user;
    }
}
